package com.quan.upms.web.controller;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.quan.common.base.bean.PageBean;
import com.quan.common.base.bean.ResultBean;
import com.quan.upms.entity.UpmsLog;
import com.quan.upms.service.UpmsLogService;
import com.quan.common.core.support.BaseController;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author 杨浩泉
 * @since 2018-06-07
 */
@RestController
@RequestMapping("/upms/log")
public class UpmsLogController extends BaseController {

    @Autowired 
    private UpmsLogService service;

	/**
     * 添加
     * @param  entity 实体
     * @return ResultBean
     */
    @PostMapping("/add")
    public Object add(@Valid @RequestBody UpmsLog entity, BindingResult bindingResult) {
    	if (bindingResult.hasErrors()) {
			return ResultBean.warning(bindingResult.getFieldError().getRejectedValue().toString());
		}
		UpmsLog insertSelective = this.service.insertSelective(entity);
		if (null != insertSelective) {
			return ResultBean.ok();
		} else {
			return ResultBean.failure();
		}
    }

    /**
     * 删除
     * @param id ID
     * @return ResultBean
     */
    @DeleteMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id) {
    	int count = this.service.deleteById(id);
    	if(count > 0) {
    		return ResultBean.ok();
    	} else {
    		return ResultBean.failure("通过ID删除失败：[ " + id + " ]");
    	}
    }
    
    /**
     * 批量删除
     * @param params idList(JSON):["1","2"]
     * @return ResultBean
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody Map<String, Object> params) {
    	if(null != params && !params.isEmpty()) {
    		List<Integer> idList = JSON.parseArray(params.get("idList").toString(), Integer.class);
    		int count = this.service.deleteByIds(StringUtils.join(idList, ","));
        	if(count > 0) {
        		return ResultBean.ok();
        	} else {
        		return ResultBean.failure("通过ID删除失败：[ " + idList + " ]");
        	}
    	}
    	return ResultBean.failure("请选项需要删除的记录");
    }

    /**
     * 修改
     * @param  entity 实体
     * @return ResultBean
     */
    @PostMapping("/update")
    public Object update(@Valid @RequestBody UpmsLog entity, BindingResult bindingResult) {
    	if (bindingResult.hasErrors()) {
            return ResultBean.warning(bindingResult.getFieldError().getDefaultMessage().toString()) ;
        }
    	int count = this.service.updateSelective(entity);
    	if(count > 0) {
    		return ResultBean.ok();
    	} else {
    		return ResultBean.failure();
    	}
    }

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return UpmsLog
    */
    @GetMapping("/get/{id}")
    public Object get(@PathVariable Integer id) {
    	UpmsLog entity = this.service.selectOneById(id);
    	if(null != entity) {
    		return ResultBean.ok(entity);
    	} else {
    		return ResultBean.failure("通过ID查询失败：[ " + id + " ]");
    	}
    }
    
    /**
    * 多条件查询数据列表
    *
    * @param entiy 实体对象（查询条件）
    * @return 分页对象
    */
    @GetMapping(value = "/list")
    public Object list(UpmsLog entity) {
		return ResultBean.ok(this.service.selectAll(entity));
    }

    /**
    * 多条件查询分页数据
    *
    * @param entiy 实体对象（查询条件）
    * @return 分页对象
    */
    @GetMapping(value = "/page")
    public Object page(UpmsLog entity) {
		return ResultBean.ok(this.service.selectPage(entity, new PageBean(request)));
    }

}
